﻿using System.Runtime.InteropServices;

namespace CommonNet48.SystemUtils
{
    /// <summary>
    /// windows休眠帮助类
    /// </summary>
    public class WinSleepCtr
    {
        //定义API函数
        [DllImport("kernel32.dll")]
        static extern uint SetThreadExecutionState(uint esFlags);
        const uint ES_SYSTEM_REQUIRED = 0x00000001;
        const uint ES_DISPLAY_REQUIRED = 0x00000002;
        const uint ES_CONTINUOUS = 0x80000000;

        /// <summary>
        /// 恢复休眠
        /// </summary>
        public static void WindowsSleep()
        {
            SetThreadExecutionState(ES_CONTINUOUS);
        }

        /// <summary>
        /// 阻止休眠
        /// </summary>
        public static void WindowsNotSleep()
        {
            SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
        }
    }
}
